package com.ruoyi.his.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.his.domain.TMedicalDrugsPharmacy;
import com.ruoyi.his.domain.TMedicalDrugsStorage;
import com.ruoyi.his.domain.vo.TMedicalDrugsPharmacyVo;
import com.ruoyi.his.service.ITMedicalDrugsPharmacyService;
import com.ruoyi.his.service.ITMedicalDrugsStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 药房Controller
 * 
 * @author ruoyi
 * @date 2021-09-10
 */
@RestController
@RequestMapping("/system/pharmacy")
public class TMedicalDrugsPharmacyController extends BaseController
{
    @Autowired
    private ITMedicalDrugsPharmacyService tMedicalDrugsPharmacyService;

    @Autowired
    private ITMedicalDrugsStorageService tMedicalDrugsStorageService;

    /**
     * 查询药房列表
     */
    @PreAuthorize("@ss.hasPermi('system:pharmacy:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMedicalDrugsPharmacyVo tMedicalDrugsPharmacy)
    {
        startPage();
        List<TMedicalDrugsPharmacyVo> list = tMedicalDrugsPharmacyService.selectTMedicalDrugsPharmacyList(tMedicalDrugsPharmacy);
        return getDataTable(list);
    }

    /**
     * 导出药房列表
     */
    @PreAuthorize("@ss.hasPermi('system:pharmacy:export')")
    @Log(title = "药房", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMedicalDrugsPharmacyVo tMedicalDrugsPharmacy)
    {
        List<TMedicalDrugsPharmacyVo> list = tMedicalDrugsPharmacyService.selectTMedicalDrugsPharmacyList(tMedicalDrugsPharmacy);
        ExcelUtil<TMedicalDrugsPharmacyVo> util = new ExcelUtil<TMedicalDrugsPharmacyVo>(TMedicalDrugsPharmacyVo.class);
        return util.exportExcel(list, "药房数据");
    }

    /**
     * 获取药房详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pharmacy:query')")
    @GetMapping(value = "/{pharmacyid}")
    public AjaxResult getInfo(@PathVariable("pharmacyid") Long pharmacyid)
    {
        return AjaxResult.success(tMedicalDrugsPharmacyService.selectTMedicalDrugsPharmacyByPharmacyid(pharmacyid));
    }

    /**
     * 新增药房
     */
    @PreAuthorize("@ss.hasPermi('system:pharmacy:add')")
    @Log(title = "药房", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMedicalDrugsPharmacy tMedicalDrugsPharmacy)
    {
        TMedicalDrugsStorage drugsId = tMedicalDrugsStorageService.getDrugsId(tMedicalDrugsPharmacy.getDrugsid());
        TMedicalDrugsPharmacy pharmacy = tMedicalDrugsPharmacyService.selectTMedicalDrugsPharmacyByPharmacyid(tMedicalDrugsPharmacy.getDrugsid());
        if(pharmacy!=null){
            if(pharmacy.getDrugsid()==tMedicalDrugsPharmacy.getDrugsid()&&pharmacy.getNamess().equals(tMedicalDrugsPharmacy.getNamess())&&drugsId.getNo()>=tMedicalDrugsPharmacy.getNo()){
                pharmacy.setNo(pharmacy.getNo()+tMedicalDrugsPharmacy.getNo());
                drugsId.setNo(drugsId.getNo()-tMedicalDrugsPharmacy.getNo());
                tMedicalDrugsStorageService.updateTMedicalDrugsStorage(drugsId);
                System.out.println("=================================="+pharmacy);
                return toAjax(tMedicalDrugsPharmacyService.updateTMedicalDrugsPharmacy(pharmacy));
            }
        }

        if(drugsId.getNo()>=tMedicalDrugsPharmacy.getNo()){
            drugsId.setNo(drugsId.getNo()-tMedicalDrugsPharmacy.getNo());
            tMedicalDrugsStorageService.updateTMedicalDrugsStorage(drugsId);
            return toAjax(tMedicalDrugsPharmacyService.insertTMedicalDrugsPharmacy(tMedicalDrugsPharmacy));
        }else{
          throw  new RuntimeException("出库失败，数量不够");
        }
    }

    /**
     * 修改药房
     */
    @PreAuthorize("@ss.hasPermi('system:pharmacy:edit')")
    @Log(title = "药房", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMedicalDrugsPharmacy tMedicalDrugsPharmacy)
    {
        //药库
        TMedicalDrugsStorage storage = tMedicalDrugsStorageService.getDrugsId(tMedicalDrugsPharmacy.getDrugsid());
        TMedicalDrugsPharmacy pharmacy = tMedicalDrugsPharmacyService.selectTMedicalDrugsPharmacyByPharmacyid(tMedicalDrugsPharmacy.getPharmacyid());
        System.out.println("======================="+storage);
        System.out.println("======================="+pharmacy);
        if(tMedicalDrugsPharmacy.getNo()<=pharmacy.getNo()){
            pharmacy.setNo(pharmacy.getNo()-tMedicalDrugsPharmacy.getNo());
            storage.setNo(tMedicalDrugsPharmacy.getNo()+storage.getNo());
            tMedicalDrugsStorageService.updateTMedicalDrugsStorage(storage);
        }else{
            throw new RuntimeException("数量不够，退库失败");
        }
        return toAjax(tMedicalDrugsPharmacyService.updateTMedicalDrugsPharmacy(pharmacy));
    }

    /**
     * 删除药房
     */
    @PreAuthorize("@ss.hasPermi('system:pharmacy:remove')")
    @Log(title = "药房", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pharmacyids}")
    public AjaxResult remove(@PathVariable Long[] pharmacyids)
    {
        return toAjax(tMedicalDrugsPharmacyService.deleteTMedicalDrugsPharmacyByPharmacyids(pharmacyids));
    }
}
